using System;
using System.Collections;
using System.Reflection;
using DataBroker.Dao;

namespace NorthwindDAO
{
	public class Order_details : Entity
	{
		private Int32 _OrderID;
		private Int32 _ProductID;
		private Decimal _UnitPrice;
		private Int16 _Quantity;
		private Single _Discount;

        public override string TableName()
        {
            return "Order details";
        }
    	
		///Default constructor
		public Order_details ()
		{
		
		}

        /// <summary>
        /// Constructor for joins. 
		/// Available PARENT tables to join with:
		///    - Orders
		///    - Products
        /// </summary>
        public Order_details(bool Flat, params Entity[] entities)
            : this()
		{
			foreach(Entity entity in entities)
			{
				string[] entityParts = entity.GetType().ToString().Split('.');
				string entityName = entityParts[entityParts.Length - 1];
				PropertyInfo pf = GetType().GetProperty((entityName));
				if (pf != null)
				{
					pf.SetValue(this,new EntityCollection(entity), null);
				}
                entityName = entityName + "_"; ;
                pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, entity, null);
                }
			}
			SetFlat(Flat);
		}
        public Order_details(params Entity[] entities)
            : this(true, entities)
		{
		}
	
		[IsNotNullable]
		[IsPK]
		public Int32 Orderid
		{
			get
			{
				return _OrderID;
			}
			set
			{
				_OrderID = value;
			}
		}
		[IsNotNullable]
		[IsPK]
		public Int32 Productid
		{
			get
			{
				return _ProductID;
			}
			set
			{
				_ProductID = value;
			}
		}
		[IsNotNullable]
		[DefaultValue("(0)")]
		public Decimal Unitprice
		{
			get
			{
				return _UnitPrice;
			}
			set
			{
				_UnitPrice = value;
			}
		}
		[IsNotNullable]
		[DefaultValue("(1)")]
		public Int16 Quantity
		{
			get
			{
				return _Quantity;
			}
			set
			{
				_Quantity = value;
			}
		}
		[IsNotNullable]
		[DefaultValue("(0)")]
		public Single Discount
		{
			get
			{
				return _Discount;
			}
			set
			{
				_Discount = value;
			}
		}
        #region Orders 
        private EntityCollection _Orders;
        [NotInTable]
        [TypeContained(typeof(Orders),"Orders")]
        public EntityCollection Orders
        {
            get
            {
                return _Orders;
            }

            set
            {
                _Orders = value;
            }
        }
        private Orders _Orders_;
        [NotInTable]
        public Orders Orders_
        {
            get
            {
                return _Orders_;
            }

            set
            {
                _Orders_ = value;
            }
        }
        #endregion
        #region Products 
        private EntityCollection _Products;
        [NotInTable]
        [TypeContained(typeof(Products),"Products")]
        public EntityCollection Products
        {
            get
            {
                return _Products;
            }

            set
            {
                _Products = value;
            }
        }
        private Products _Products_;
        [NotInTable]
        public Products Products_
        {
            get
            {
                return _Products_;
            }

            set
            {
                _Products_ = value;
            }
        }
        #endregion
        public override DBJoin GetJoin(string tableName)
        {
	        if (string.Compare(tableName, "Orders", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
				left.Add(tableName + ".Orderid");
				right.Add(GetType().Name + ".Orderid");
				return new DBJoin(left, right, JoinTypeEnumeration.INNER);
			}
	        if (string.Compare(tableName, "Products", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
				left.Add(tableName + ".Productid");
				right.Add(GetType().Name + ".Productid");
				return new DBJoin(left, right, JoinTypeEnumeration.INNER);
			}
			return null;
		}
	}
}


